﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OBDUDSUtils.EtasCAN
{
    class Etas581
    {
        public static int icsneoFindNeoDevices(UInt32 DeviceTypes, ref NeoDevice pNeoDevice, ref Int32 pNumDevices)
        {
            //Search for connected hardware
            return icsNeoDll.icsneoFindNeoDevices(DeviceTypes, ref pNeoDevice, ref pNumDevices);
        }

        public static int icsneoFindDevices(ref NeoDeviceEx pNeoDevice, ref Int32 pNumDevices, UInt32 DeviceTypes, UInt32 numDeviceTypes, ref OptionsNeoEx pOptionsFindNeoEx, UInt32 reserved)
        {
            return icsNeoDll.icsneoFindDevices(ref pNeoDevice, ref pNumDevices, DeviceTypes, numDeviceTypes, ref pOptionsFindNeoEx, reserved);
        }

        public static int icsneoOpenNeoDevice(ref NeoDevice pNeoDevice, ref IntPtr hObject, ref byte[] bNetworkIDs, Int32 bConfigRead, Int32 bSyncToPC)
        {
            return icsNeoDll.icsneoOpenNeoDevice(ref pNeoDevice, ref hObject, ref bNetworkIDs[0], bConfigRead, bSyncToPC);
        }

        public static int icsneoSerialNumberToString(UInt32 Serial, ref byte Data, ref UInt32 DataSize)
        {
            return icsNeoDll.icsneoSerialNumberToString(Serial, ref Data, ref DataSize);
        }

        public static byte GetNetworkIDfromString(string sTempString)
        {
            return Convert.ToByte(icsNeoDll.GetNetworkIDfromString(ref sTempString));
        }

        public static int icsneoClosePort(IntPtr hObject, ref Int32 pNumberOfErrors)
        {
            return icsNeoDll.icsneoClosePort(hObject, ref pNumberOfErrors);
        }

        public static void icsneoFreeObject(IntPtr hObject)
        {
            icsNeoDll.icsneoFreeObject(hObject);
        }

        public static int icsneoOpenPortEx(Int32 lPortNumber, Int32 lPortType, Int32 lDriverType, Int32 lIPAddressMSB, Int32 lIPAddressLSBOrBaudRate, Int32 bConfigRead, ref byte bNetworkID, ref IntPtr hObject)
        {
            return icsNeoDll.icsneoOpenPortEx(lPortNumber, lPortType, lDriverType, lIPAddressMSB, lIPAddressLSBOrBaudRate, bConfigRead, ref bNetworkID, ref hObject);
        }

        public static int icsneoTxMessages(IntPtr hObject, ref icsSpyMessage pMsg, Int32 lNetworkID, Int32 lNumMessages)
        {
            return icsNeoDll.icsneoTxMessages(hObject, ref pMsg, lNetworkID, lNumMessages);
        }

        public static int icsneoTxMessages(IntPtr hObject, ref icsSpyMessageJ1850 pMsg, Int32 lNetworkID, Int32 lNumMessages)
        {
            return icsNeoDll.icsneoTxMessages(hObject, ref pMsg, lNetworkID, lNumMessages);
        }

        public static int icsneoTxMessagesEx(IntPtr hObject, ref icsSpyMessage pMsg, UInt32 lNetworkID, UInt32 lNumMessages, ref UInt32 NumTxed, UInt32 reserved)
        {
            return icsNeoDll.icsneoTxMessagesEx(hObject, ref pMsg, lNetworkID, lNumMessages, ref NumTxed, reserved);
        }

        public static int icsneoISO15765Message(IntPtr m_hObject, int iNetwork, ref stCM_ISO157652_TxMessage txMsg, int blockTimeout)
        {
            return icsNeoDll.icsneoISO15765_TransmitMessage(m_hObject, iNetwork, ref txMsg, blockTimeout);
        }

        public static int icsneoGetMessages(IntPtr hObject, ref icsSpyMessage[] pMsg, ref Int32 pNumberOfMessages, ref Int32 pNumberOfErrors)
        {
            return icsNeoDll.icsneoGetMessages(hObject, ref pMsg[0], ref pNumberOfMessages, ref pNumberOfErrors);
        }

        public static int icsneoGetTimeStampForMsg(IntPtr hObject, ref icsSpyMessage pMsg, ref double pTimeStamp)
        {
            return icsNeoDll.icsneoGetTimeStampForMsg(hObject, ref pMsg, ref pTimeStamp);
        }

        public static Int32 icsneoWaitForRxMessagesWithTimeOut(IntPtr hObject, UInt32 iTimeOut)
        {
            return icsNeoDll.icsneoWaitForRxMessagesWithTimeOut(hObject, iTimeOut);
        }

        public static Int32 icsneoEnableNetworkRXQueue(IntPtr hObject, Int32 iEnable)
        {
            return icsNeoDll.icsneoEnableNetworkRXQueue(hObject, iEnable);
        }

        public static void icsneoGetISO15765Status(IntPtr hObject, Int32 lNetwork, Int32 lClearTxStatus, Int32 lClearRxStatus, ref Int32 lTxStatus, ref Int32 lRxStatus)
        {
            icsNeoDll.icsneoGetISO15765Status(hObject, lNetwork, lClearTxStatus, lClearRxStatus, ref lTxStatus, ref lRxStatus);
        }

        public static int icsneoGetConfiguration(IntPtr hObject, ref byte pData, ref Int32 lNumBytes)
        {
            return icsNeoDll.icsneoGetConfiguration(hObject, ref pData, ref lNumBytes);
        }

        public static int icsneoSendConfiguration(IntPtr hObject, ref byte pData, Int32 lNumBytes)
        {
            return icsNeoDll.icsneoSendConfiguration(hObject, ref pData, lNumBytes);
        }

        public static int icsneoGetFireSettings(IntPtr hObject, ref SFireSettings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetFireSettings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetFireSettings(IntPtr hObject, ref SFireSettings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetFireSettings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetVCAN3Settings(IntPtr hObject, ref SVCAN3Settings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetVCAN3Settings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetVCAN3Settings(IntPtr hObject, ref SVCAN3Settings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetVCAN3Settings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetVCAN412Settings(IntPtr hObject, ref SVCAN412Settings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetVCAN412Settings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetVCAN412Settings(IntPtr hObject, ref SVCAN412Settings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetVCAN412Settings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetFire2Settings(IntPtr hObject, ref SFire2Settings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetFire2Settings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetFire2Settings(IntPtr hObject, ref SFire2Settings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetFire2Settings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetRADStar2Settings(IntPtr hObject, ref SRADStar2Settings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetRADStar2Settings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetRADStar2Settings(IntPtr hObject, ref SRADStar2Settings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetRADStar2Settings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetRADGalaxySettings(IntPtr hObject, ref SRADGalaxySettings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetRADGalaxySettings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetRADGalaxySettings(IntPtr hObject, ref SRADGalaxySettings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetRADGalaxySettings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetVCANRFSettings(IntPtr hObject, ref SVCANRFSettings pSettings, Int32 iNumBytes)
        {
            return icsNeoDll.icsneoGetVCANRFSettings(hObject, ref pSettings, iNumBytes);
        }

        public static int icsneoSetVCANRFSettings(IntPtr hObject, ref SVCANRFSettings pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetVCANRFSettings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM);
        }

        public static int icsneoGetDeviceSettings(IntPtr hObject, ref SRADPlutoSettingsPack pSettings, Int32 iNumBytes, Int32 VnetChan)
        {
            return icsNeoDll.icsneoGetDeviceSettings(hObject, ref pSettings, iNumBytes, VnetChan);
        }

        public static int icsneoSetDeviceSettings(IntPtr hObject, ref SRADPlutoSettingsPack pSettings, Int32 iNumBytes, Int32 bSaveToEEPROM, Int32 VnetChan)
        {
            return icsNeoDll.icsneoSetDeviceSettings(hObject, ref pSettings, iNumBytes, bSaveToEEPROM, VnetChan);
        }

        public static Int32 icsneoSetBitRate(IntPtr hObject, Int32 BitRate, Int32 NetworkID)
        {
            return icsNeoDll.icsneoSetBitRate(hObject, BitRate, NetworkID);
        }

        public static Int32 icsneoSetFDBitRate(IntPtr hObject, Int32 BitRate, Int32 NetworkID)
        {
            return icsneoSetFDBitRate(hObject, BitRate, NetworkID);
        }

        public static int icsneoGetDeviceParameters(IntPtr hObject, ref char pParameter, ref char pValues, Int16 ValuesLength)
        {
            return icsNeoDll.icsneoGetDeviceParameters(hObject, ref pParameter, ref pValues, ValuesLength);
        }

        public static int icsneoSetDeviceParameters(IntPtr hObject, ref char pParmValue, ref Int32 pErrorIndex, Int32 bSaveToEEPROM)
        {
            return icsNeoDll.icsneoSetDeviceParameters(hObject, ref pParmValue, ref pErrorIndex, bSaveToEEPROM);
        }

        public static Int32 icsneoGetLastAPIError(IntPtr hObject, ref UInt32 pErrorNumber)
        {
            return icsNeoDll.icsneoGetLastAPIError(hObject, ref pErrorNumber);
        }

        public static int icsneoGetErrorMessages(IntPtr hObject, ref Int32 pErrorMsgs, ref Int32 pNumberOfErrors)
        {
            return icsNeoDll.icsneoGetErrorMessages(hObject, ref pErrorMsgs, ref pNumberOfErrors);
        }

        public static int icsneoGetErrorInfo(int iErrorNumber, string sErrorDescriptionShortStr, string sErrorDescriptionLongStr, ref int iMaxLengthShort, ref int iMaxLengthLong, ref int lErrorSeverity, ref int lRestartNeeded)
        {
            NeoResult result = new NeoResult();
            StringBuilder sErrorDescriptionShort = new StringBuilder();
            sErrorDescriptionShort.Append(sErrorDescriptionShortStr);
            StringBuilder sErrorDescriptionLong = new StringBuilder();
            sErrorDescriptionLong.Append(sErrorDescriptionLongStr);

            return icsNeoDll.icsneoGetErrorInfo(iErrorNumber, sErrorDescriptionShort, sErrorDescriptionLong, ref iMaxLengthShort, ref iMaxLengthLong, ref lErrorSeverity, ref lRestartNeeded);
        }

        public static Int32 icsneoValidateHObject(IntPtr hObject)
        {
            return icsNeoDll.icsneoValidateHObject(hObject);
        }

        public static Int32 icsneoGetDLLVersion()
        {
            return icsNeoDll.icsneoGetDLLVersion();
        }

        public static Int32 icsneoStartSockServer(IntPtr hObject, Int32 iPort)
        {
            return icsNeoDll.icsneoStartSockServer(hObject, iPort);
        }

        public static Int32 icsneoStopSockServer(IntPtr hObject)
        {
            return icsNeoDll.icsneoStopSockServer(hObject);
        }

        public static Int32 icsneoScriptStart(IntPtr hObject, Int32 iLocation)
        {
            return icsNeoDll.icsneoScriptStart(hObject, iLocation);
        }

        public static Int32 icsneoScriptStop(IntPtr hObject)
        {
            return icsNeoDll.icsneoScriptStop(hObject);
        }

        public static int icsneoScriptLoad(IntPtr hObject, ref byte bin, UInt32 len_bytes, Int32 iLocation)
        {
            return icsNeoDll.icsneoScriptLoad(hObject, ref bin, len_bytes, iLocation);
        }

        public static Int32 icsneoScriptClear(IntPtr hObject, Int32 iLocation)
        {
            return icsNeoDll.icsneoScriptClear(hObject, iLocation);
        }

        public static Int32 icsneoScriptStartFBlock(IntPtr hObject, UInt32 fb_index)
        {
            return icsNeoDll.icsneoScriptStartFBlock(hObject, fb_index);
        }

        public static int icsneoScriptGetFBlockStatus(IntPtr hObject, UInt32 fb_index, ref Int32 piRunStatus)
        {
            return icsNeoDll.icsneoScriptGetFBlockStatus(hObject, fb_index, ref piRunStatus);
        }

        public static Int32 icsneoScriptStopFBlock(IntPtr hObject, UInt32 fb_index)
        {
            return icsNeoDll.icsneoScriptStopFBlock(hObject, fb_index);
        }

        public static int icsneoScriptGetScriptStatus(IntPtr hObject, ref Int32 piStatus)
        {
            return icsNeoDll.icsneoScriptGetScriptStatus(hObject, ref piStatus);
        }

        public static int icsneoOpenPort(Int32 lPortNumber, Int32 lPortType, Int32 lDriverType, ref byte bNetworkID, ref byte bSCPIDs, ref IntPtr hObject)
        {
            return icsNeoDll.icsneoOpenPort(lPortNumber, lPortType, lDriverType, ref bNetworkID, ref bSCPIDs, ref hObject);
        }

        public static Int32 icsneoEnableNetworkCom(IntPtr hObject, Int32 Enable)
        {
            return icsNeoDll.icsneoEnableNetworkCom(hObject, Enable);
        }

        public static Int32 icsneoISO15765_DisableNetworks(IntPtr hObject)
        {
            return icsNeoDll.icsneoISO15765_DisableNetworks(hObject);
        }

        public static int icsneoISO15765_EnableNetworks(IntPtr m_hObject, int iNetwork)
        {
            return icsNeoDll.icsneoISO15765_EnableNetworks(m_hObject, iNetwork);
        }

        public static int icsneoISO15765_ReceiveMessage(IntPtr hObject, Int32 ulNetworkID, ref stCM_ISO157652_RxMessage pMsg)
        {
            return icsNeoDll.icsneoISO15765_ReceiveMessage(hObject, ulNetworkID, ref pMsg);
        }

        public static int icsneoReadWritePHYSettings(IntPtr hObject, ref PhyRegPkt_t PHYSettings, IntPtr size, IntPtr NumEntries)
        {
            return icsNeoDll.icsneoReadWritePHYSettings(hObject, ref PHYSettings, size, NumEntries);
        }

        public static int icsneoFindAllCOMDevices(Int32 lDriverType, Int32 lGetSerialNumbers, Int32 lStopAtFirst, Int32 lUSBCommOnly, ref Int32 p_lDeviceTypes, ref Int32 p_lComPorts, ref Int32 p_lSerialNumbers, ref Int32 lNumDevices)
        {
            return icsNeoDll.icsneoFindAllCOMDevices(lDriverType, lGetSerialNumbers, lStopAtFirst, lUSBCommOnly, ref p_lDeviceTypes, ref p_lComPorts, ref p_lSerialNumbers, ref lNumDevices);
        }

        public static string GetStringForNetworkID(Int16 lNetworkID)
        {
            return icsNeoDll.GetStringForNetworkID(lNetworkID);
        }

        public static void GetMessageDataBuffer(icsSpyMessageClass[] stMessages, ref byte[] iDataBytes, int iLongMessageTotalByteCount, int lCount)
        {
            System.Runtime.InteropServices.Marshal.Copy(stMessages[lCount - 1].iExtraDataPtr, iDataBytes, 0, iLongMessageTotalByteCount);
            System.Runtime.InteropServices.GCHandle gcHandle = System.Runtime.InteropServices.GCHandle.Alloc(iDataBytes, System.Runtime.InteropServices.GCHandleType.Pinned);
            gcHandle.Free();
        }

        public static void ConvertCANtoJ1850Message(ref icsSpyMessage stMessages, ref icsSpyMessageJ1850 stJMsg)
        {
            icsNeoDll.ConvertCANtoJ1850Message(ref stMessages, ref stJMsg);
        }

        public static icsSpyMessage[] getICSSpyMessageArray(int arraySize)
        {
            icsSpyMessage[] stMessages = new icsSpyMessage[arraySize];   //TempSpace for messages
            return stMessages;
        }

        public static IntPtr GetIntPtr()
        {
            return new IntPtr();
        }
    }
}
